"""
@project:购物系统.py
@author:zhujiahui
@IDE:PyCharm
@time:2021-09-02-22:53
@Introduction:
"""
"""
装饰器装饰类
●定义-个装饰器sorted_by_create_ time实现装饰类的装饰器
●装饰器的功能:按创建时间将实例进行排序
"""

# class sorted_by_create_time(object):
#     def __init__(self, fn):
#         # 初始化操作在此完成
#         self.__fn = fn
#
#     # 实现__call__方法，表示对象是一个可调用对象，可以像调用函数一样进行调用。
#     def __call__(self, *args, **kwargs):
#         # 添加装饰功能
#         i = 0
#         print("即将按照创建时间对实例进行排序...")
#         print(f"这是您创建的第{i}个实例")
#         self.__fn()
#
#
# @sorted_by_create_time
# def dx_range():
#     print("创建完成！")
#
# dx_range()
# dx_range(2)













# #购物系统
# import json,sys
# def login_required(func):
#     def _login_required(*args):
#         if username == None:
#             print('用户未登录，请登陆后重试！')
#         else:
#             func(*args)
#     return  _login_required
#
# def operate():
#     print('==========用户操作===========')
#     print('==1.登录')
#     print('==2.注册')
#     print('==3.购物')
#     print('==4.充值')
#     print('==5.查看余额')
#     print('==6.查看购物清单')
#     print('==7.查看商品列表')
#     print('=============================')
#
# def menu():
#     global  goods_menu
#     goods_menu={'芒果':7,
#                 '火龙果':5,
#                 '香蕉':3,
#                 '苹果':5,
#                 '梨子':3,
#                 '桃子':3
#                 }
#     print('==========商品信息===========')
#     i=1
#     for name,price in goods_menu.items():
#         print(f'{i}.{name}——{price}元/斤')
#         i+=1
#     print('=============================')
#
# def register():
#     print('============注册=============')
#     name = input('请输入用户名：')
#     user_exit(name)
#     with open('user.txt','r',encoding='utf-8') as f:
#         s = f.read()
#         user_dict = json.loads(s)
#     if name in user_dict:
#         print('用户名已存在')
#     else:
#         pwd = input('请输入密码：')
#         user_exit(pwd)
#         with open('user.txt', 'w', encoding='utf-8') as f:
#             user_dict[name] = pwd
#             user_str=json.dumps(user_dict)
#             f.write(user_str)
#         with open('user_count.txt', 'r', encoding='utf-8') as f:
#             s = f.read()
#             user_count_dict = json.loads(s)
#         with open('user_count.txt','w',encoding='utf-8') as f:
#             user_count_dict[name] = 0
#             f.write(json.dumps(user_count_dict))
#             print('注册成功!')
#
# def login():
#     if username == None:
#         with open('user.txt','r',encoding='utf-8') as f:
#             user_str = f.read()
#             user_dict = json.loads(user_str)
#             while True:
#                 name = input('请输入用户名：')
#                 user_exit(name)
#                 if name in set(user_dict) :
#                     pwd = input('请输入密码：')
#                     user_exit(pwd)
#                     if user_dict[name] == pwd:
#                         print('登陆成功！')
#                         return name
#                     else:
#                         print('密码错误！')
#                 else:
#                     print('用户名未注册！')
#     else:
#         print('请勿重复登录！')
#
#
# @login_required
# def user_shop(user_name):
#     while True:
#         menu()
#         name = input('请输入您要买的商品名称[苹果](按q结束购买)：')
#         user_exit(name)
#         if name == 'q':
#             break
#         if name not in goods_menu:
#             print('输入不合法，请重新输入！')
#             continue
#         else:
#             num = input('请输入需要购买的斤数：')
#             user_exit(num)
#         if num.isdigit():
#             with open('user_count.txt', 'r', encoding='utf-8') as f:
#                 s = f.read()
#                 user_count_dict = json.loads(s)
#                 if user_count_dict[user_name] - int(num) * goods_menu[name] < 0:
#                     print('余额不足！请充值！')
#                 else:
#                     if name not in goods_list:
#                         goods_list[name] = 0
#                     goods_list[name] += int(num)
#                     with open('user_count.txt', 'w', encoding='utf-8') as f:
#                         user_count_dict[user_name] -= int(num) * goods_menu[name]
#                         f.write(json.dumps(user_count_dict))
#                         print('购买成功！')
#         else:
#             print('输入不合法！')
#
# @login_required
# def recharge(name):
#     money = input('请输入充值金额：')
#     user_exit(money)
#     with open('user_count.txt','r',encoding='utf-8') as f:
#         s = f.read()
#         user_count_dict = json.loads(s)
#         user_count_dict[name] += int(money)
#         print(f'当前余额为:￥{user_count_dict[name]}')
#
#     with open('user_count.txt', 'w', encoding='utf-8') as f:
#         f.write(json.dumps(user_count_dict))
#
# @login_required
# def balance(name):
#     with open('user_count.txt', 'r', encoding='utf-8') as f:
#         s = f.read()
#         user_count_dict = json.loads(s)
#         print(f'当前余额为:￥{user_count_dict[name]}')
#
# @login_required
# def shop_list():
#     if len(goods_list) == 0:
#         print('————您没有购买任何东西！————')
#     else:
#         print('———————购物清单———————')
#         print('序号        商品      单价  斤      总价')
#         i=1
#         all_counts = 0
#         for name, num in goods_list.items():
#             counts = int(goods_menu[name])*int(num)
#             all_counts += counts
#             print(f'{i}     {name}      {goods_menu[name]}元/斤       {num}斤      {counts}元')
#             i += 1
#         print(f'合计                           {all_counts}元')
#         print('——————————————————')
#         # return all_counts
#
# def user_exit(user_input):
#     if user_input == 'exit':
#         shop_list()
#         print('退出！')
#         sys.exit()
#
# goods_menu = {}#商品菜单
# goods_list = {}#用户购物清单
# username = None
#
# while True:
#     operate()
#     choice = input('请输入(任意位置输入exit退出)：')
#     user_exit(choice)
#     if choice == '1':
#         username=login()
#     elif choice == '2':
#         register()
#     elif choice == '3':
#         user_shop(username)
#     elif choice == '4':
#         recharge(username)
#     elif choice == '5':
#         balance(username)
#     elif choice == '6':
#         shop_list()
#     elif choice == '7':
#         menu()
#     elif choice == 'exit':
#         user_exit(choice)
#     else:
#         print('输入不合法！请您重新输入！')